<template>
  <div :style="conheight">
    <el-scrollbar :style="conheight">
      <!-- <el-card shadow="never"
               height="100%"> -->
      <el-input v-model="logFileContent"
                type="textarea"
                :autosize="inputAutoSize"></el-input>
      <!-- </el-card> -->
    </el-scrollbar>
  </div>
</template>
<script>
import { formatDate } from "@/utils/date";
import { hasPermission, getEntries, getLabel } from "@/utils/dict";
import { apiReadTaskLog } from "@/api/monitor";


export default {
  name: "TaskLogFile",
  props: {
    myLogFile: {
      logFileName: '默认值',
      hostName: 'yourHost',
    }
  },
  data() {
    return {
      intervalReader: null,
      conheight: {
        height: ''
      },
      inputAutoSize: {
        minRows: 20,
        maxRows: 20
      },
      logFileContent: "",
      startLineNbr: 0,
      finished: false,
    };
  },

  created() {
    window.addEventListener('resize', this.getHeight);
    this.getHeight();
  },

  mounted() {
    if (this.intervalReader) {
      clearInterval(this.intervalReader);
      this.intervalReader = null;
    }

    this.intervalReader = setInterval(() => {
      setTimeout(this.handleReadLogFile(), 0)
    }, 1000);
  },

  destroyed() {
    if (this.intervalReader) {
      clearInterval(this.intervalReader);
      this.intervalReader = null;
    }
  },


  methods: {
    hasPermission(url) {
      return hasPermission(url)
    },
    getOptions(dictKey) {
      return getEntries(dictKey);
    },

    getDictLabel(dictKey, dictValue) {
      return getLabel(dictKey, dictValue);
    },

    getHeight() {
      let height = window.innerHeight - 230;
      this.conheight.height = height + 'px';
      var rows = Math.floor((height - 20) / 20);

      this.$set(this.inputAutoSize, 'minRows', rows);
      this.$set(this.inputAutoSize, 'maxRows', rows);
    },

    handleReadLogFile() {
      this.listLoading = true;

      let param = {}
      param.hostName = this.myLogFile.hostName;
      param.startLineNbr = this.startLineNbr;
      param.fileName = this.myLogFile.logFileName;

      // 返回
      // private Integer lastLineNbr;
      // private String lines;
      apiReadTaskLog(param)
        .then(response => {
          this.listLoading = false;
          const data = response.data;
          if (data) {
            var respObj = JSON.parse(data);
            if (respObj.lastLineNbr < 0) {
              if (!this.myLogFile.isRunning) {
                clearInterval(this.intervalReader);
                this.intervalReader = null;
                this.myLogFile.iconClass = null;
              }
            } else {
              this.startLineNbr = respObj.lastLineNbr;
              let lines = decodeURIComponent(respObj.lines);
              this.logFileContent = this.logFileContent + lines;
              if (lines.endsWith("MANDY_CTL_LOG_END.\n")) {
                clearInterval(this.intervalReader);
                this.intervalReader = null;
                this.myLogFile.iconClass = null;
                if (this.myLogFile.isRunning) {
                  this.$message({
                    type: 'info',
                    message: '任务ID:' + this.myLogFile.id + ' 结束啦.',
                    duration: 3000
                  });
                }
              }
            }
          }
        });
      this.listLoading = false;
    }
  }
};
</script>

<style>
.c-Button {
  border: none;
  border-radius: 2px;
}

.c-Button--close {
  background-color: red;
}
</style>

<style module>
.button {
  border: none;
  border-radius: 2px;
}

.buttonClose {
  background-color: red;
}
</style>
